package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.QaListBean;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.ImgDetailActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 15/12/28.
 */
public class QuestionListAdapter extends MyAdapter<QaListBean.DataBean> {

    List<QaListBean.DataBean> mDataBeanList;
    OnAnswerListener onAnswerListener;

    public QuestionListAdapter(Context context, List<QaListBean.DataBean> list) {
        super(context, list);

        this.mDataBeanList = list;
    }

    @Override
    public int getCount() {
        return mDataBeanList.size();
    }

    @Override
    public QaListBean.DataBean getItem(int position) {
        return mDataBeanList.get(position);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_question_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mQuestionContent.setText(getItem(position).getContent());

        Glide.with(parent.getContext())
                .load(getItem(position).getUthumb())
                .transform(new GlideCircle(parent.getContext()))
                .into(holder.mQuestionIcon);

        long time = Long.parseLong(getItem(position).getTimeline());
        Date date = DeviceUtil.getDateFromMillis(time);
        holder.mQuestionTime.setText(DeviceUtil.getTimeByTwoTimeStampForPhp(time * 1000, System.currentTimeMillis()));

        holder.mQuestionName.setText(getItem(position).getNickname());

        holder.replyCount.setText(String.valueOf(getItem(position).getReply()));

        holder.btnAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAnswerListener.answer(getItem(position).getId(), getItem(position).getReply());
            }
        });

        if (list.get(position).getPhoto() != null && !list.get(position).getPhoto().isEmpty()) {
            holder.mQuesLayout.setVisibility(View.VISIBLE);
            holder.mQuesLayout.getChildAt(0).setVisibility(View.INVISIBLE);
            holder.mQuesLayout.getChildAt(1).setVisibility(View.INVISIBLE);
            holder.mQuesLayout.getChildAt(2).setVisibility(View.INVISIBLE);
            for (int i = 0; i < list.get(position).getPhoto().size(); i++) {
                holder.mQuesLayout.getChildAt(i).setVisibility(View.VISIBLE);
                Glide
                        .with(context)
                        .load(list.get(position).getPhoto().get(i))
                        .placeholder(R.mipmap.banner_empty)
                        .into((ImageView) (holder.mQuesLayout.getChildAt(i)));
                holder.mQuesLayout.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(parent.getContext(), ImgDetailActivity.class);
                        intent.putStringArrayListExtra(Constant.IMG_DETAIL_URL, (ArrayList<String>) list.get(position).getPhoto());
                        parent.getContext().startActivity(intent);
                    }
                });
            }
        } else {
            holder.mQuesLayout.setVisibility(View.GONE);
        }
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.question_content)
        TextView mQuestionContent;
        @Bind(R.id.ques_layout)
        LinearLayout mQuesLayout;
        @Bind(R.id.question_time)
        TextView mQuestionTime;
        @Bind(R.id.question_icon)
        ImageView mQuestionIcon;
        @Bind(R.id.question_name)
        TextView mQuestionName;
        @Bind(R.id.reply_count)
        TextView replyCount;
        @Bind(R.id.btn_answer)
        LinearLayout btnAnswer;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnAnswerListener {
        void answer(String id, int reply);
    }

    public void setOnAnswerListener(OnAnswerListener onAnswerListener) {
        this.onAnswerListener = onAnswerListener;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_question_list.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */

}
